import {Button, Form, Input} from "antd";
import FormItem from "antd/es/form/FormItem";
import RequestUtils from "../../utils/RequestUtils";
import Apis from "../../configs/Apis";

const RoleQuery = (props) => {

    const {messageApi, doPageQuerySysRoles, pageQueryParam, setPageQueryParam, setRoleWriteModalParam} = props;

    /**
     * 清空查询条件
     */
    const doCleanPageQueryReq = () => {
        setPageQueryParam({
            ...pageQueryParam,
            roleName: ""
        });
        doPageQuerySysRoles({
            ...pageQueryParam,
            roleName: ""
        })
    }

    /**
     * 刷新角色权限缓存信息
     */
    const doRefreshRolePermissionsCache = () => {
        RequestUtils.post(Apis.permission.refreshCachePermissions, {})
            .then(res => {
                messageApi.success("刷新成功");
            }).catch(err => {
            messageApi.error("刷新失败, " + err.message);
        })
    }

    return (
        <div>
            <Form>

                <FormItem>
                    <label>角色名：</label>
                    <Input
                        placeholder={'请输入角色名称'}
                        style={{width: '300px'}}
                        value={pageQueryParam.roleName}
                        onChange={(e) => setPageQueryParam({
                            ...pageQueryParam,
                            roleName: e.target.value
                        })}
                    />
                </FormItem>

                <FormItem>
                    <Button
                        type={'primary'}
                        onClick={() => doPageQuerySysRoles()}>
                        查询
                    </Button>
                    <Button
                        danger={true}
                        style={{marginLeft: '15px'}}
                        onClick={() => doCleanPageQueryReq()}>
                        清空条件
                    </Button>
                    <Button
                        color="purple"
                        variant="solid"
                        style={{marginLeft: '15px'}}
                        onClick={() => {
                            setRoleWriteModalParam({
                                visible: true,
                                isEdit: false,
                                roleInfo: {}
                            })
                        }}
                    >
                        新增角色
                    </Button>
                    <Button
                        color="cyan"
                        variant="solid"
                        style={{marginLeft: '15px'}}
                        onClick={() => doRefreshRolePermissionsCache()}
                    >
                        同步缓存
                    </Button>
                </FormItem>
            </Form>

        </div>
    )
}
export default RoleQuery;